<?php
/**
 * Created by PhpStorm.
 * User: tangyunfei
 * Date: 2018/4/18
 * Time: 9:28
 */
class GgAction extends  CommentAction{
    private  $table_name;
    function __construct(){

        $this->table_name = "Gg";
        parent::__construct();
    }

    function index(){
        import('ORG.Util.Page');

        $count=M($this->table_name)->count();// 查询总数据记录

        $Page = new Page($count,C('PAGESIZE'));

        $show= $Page->show();

        $list = M($this->table_name)->order("id DESC")->limit($Page->firstRow.','.$Page->listRows)->select();


        $this->assign("list",$list);

        $this->assign("page",$show);

        $this->display();
    }

    function add(){
        if(isset($_POST['submit'])){
              $tools = new ToosModel();
              $photo = $tools->upfile();
              $data['coentent'] = $photo;
              M('Gg')->add($data);
              $this->success("操作成功",U('index'));
        }else{
            $this->display();
        }
    }

    function del(){
        $id = I('get.id');
        M('Gg')->where(array('id'=>$id))->delete();
        $this->success("操作成功",U('index'));
    }

    function edit()
    {
        $id = I('get.id');
        if(isset($_POST['submit'])){

            if(empty($_FILES['photo']['name'])){
                $data['content'] = $_POST['content'];
            }else{

                $photo = $this->upfile();
                $data['content'] = $photo;
            }

            M('Gg')->where(array('id'=>$id))->save($data);
            $this->success("操作成功",U('index'));
        }else{
            $this->info = M('Gg')->where(array('id'=>$id))->find();
            $this->display();
        }
    }

    function upfile(){

        import('ORG.Net.UploadFile');
        $path=date("Ym").'/';
        $upload = new UploadFile();// 实例化上传类
        $upload->maxSize  = 3145728 ;// 设置附件上传大小
        $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath =  './Data/photo/'.$path;// 设置附件上传目录
        if(!file_exists("./Data/photo/".$path)){
            mkdir("./Data/photo/".$path);
        }
        if(!$upload->upload()) {
            // 上传错误提示错误信息

            return $upload->getErrorMsg();
        }else{

            $info = $upload->getUploadFileInfo(); //获取上传结果信息
            $photopath= str_replace(".","",$info[0]['savepath']);
            $s=$photopath.$info[0]['savename'];

            return $s;



        }
    }

}